package codingforgreat.lchot150;

public class Lc200 {
    public int numIslands(char[][] grid) {
        int ans = 0;
        for(int i = 0;i < grid.length;i++){
            for(int j = 0; j < grid[0].length;j++){
                if(grid[i][j] == '1'){
                    dfs(grid,i,j);
                    ans++;
                }
            }
        }
        return ans;
    }
    public void dfs(char[][] grid,int i,int j){
        int N = grid.length;
        int M = grid[0].length;
        if(i < 0 || i >= N || j < 0 || j >= M || grid[i][j] != '1'){
            return;
        }
        grid[i][j] = '0';
        dfs(grid,i + 1,j);
        dfs(grid,i - 1,j);
        dfs(grid,i,j + 1);
        dfs(grid,i,j - 1);
    }
}
